﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IdentityDemo.Models;
using Microsoft.AspNetCore.Mvc;

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;

namespace IdentityDemo.Controllers
{
    public class UserController : Controller
    {
        private UserManager<User> _userManager;
        private SignInManager<User>  _signInManager;

        public UserController(UserManager<User> userManager, SignInManager<User> signInManager)
        {
            _userManager = userManager;
            _signInManager = signInManager;
        }
        public IActionResult Singin()
        {
            return View();
        }

        public IActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Login(RegisterViewModel registerViewModel)
        {
            var user = await _userManager.FindByEmailAsync("123@qq.com");

            if (user == null)
            {

            }

            await _signInManager.SignInAsync(user, new AuthenticationProperties { IsPersistent = true });

            return RedirectToAction("About", "Home");

        }

        [HttpPost]
        public  IActionResult Logout()
        {
            HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

            return RedirectToAction("Index", "Home");
        }

        public IActionResult Register()
        {
            return View();
        }
        [HttpPost]
        public async Task< IActionResult> Register(RegisterViewModel registerViewModel)
        {
            var user = new User { Email = "123@qq.com", UserName = "123" };

            var result = await _userManager.CreateAsync(user, "a.123456");

            if (result.Succeeded)
            {


            }

            return View();
        }
    }
}